package com.guigu.eduservice.mapper;

import com.guigu.eduservice.dto.MonthlyStatsDTO;
import com.guigu.eduservice.entity.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单表（存储买卖双方的交易订单信息） Mapper 接口
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 获取最近6个月订单数据
     * @return
     */
    @Select("SELECT DATE_FORMAT(create_time, '%Y-%m') AS month,\n" +
            "            COUNT(*) AS count,\n" +
            "            SUM(order_amount) AS amount\n" +
            "        FROM `order`\n" +
            "        WHERE order_status = 6\n" +
            "          AND create_time >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)\n" +
            "        GROUP BY DATE_FORMAT(create_time, '%Y-%m')\n" +
            "        ORDER BY month")
    List<MonthlyStatsDTO> getMonthlyTransactionAmount();

    /**
     * 获取今日已付款订单数量
     */
    @Select("SELECT COUNT(*) FROM `order` WHERE DATE(create_time) = CURDATE() AND order_status = 3;")
    int getTodayPaidOrderCount();

    /**
     * 获取今日已付款订单总金额
     */
    @Select("SELECT SUM(order_amount) FROM `order` WHERE DATE(create_time) = CURDATE() AND order_status = 3;")
    BigDecimal getTodayPaidAmount();
}
